package com.dp.mallchat.common.user.controller;


import com.dp.mallchat.common.common.domain.dto.RequestInfo;
import com.dp.mallchat.common.common.domain.vo.resp.ApiResult;
import com.dp.mallchat.common.common.utils.AssertUtil;
import com.dp.mallchat.common.common.utils.RequestHolder;
import com.dp.mallchat.common.user.domain.user.enums.RoleEnum;
import com.dp.mallchat.common.user.domain.user.req.*;
import com.dp.mallchat.common.user.domain.user.resp.BadgeResp;
import com.dp.mallchat.common.user.domain.user.resp.ItemInfoDTO;
import com.dp.mallchat.common.user.domain.user.resp.SummeryInfoDTO;
import com.dp.mallchat.common.user.domain.user.resp.UserInfoResp;
import com.dp.mallchat.common.user.service.RoleService;
import com.dp.mallchat.common.user.service.UserService;
import com.dp.mallchat.transaction.service.MQProducter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author <a href="https://github.com/adu">adu</a>
 * @since 2024-05-14
 */
@Api(value = "用户相关接口", tags = "用户相关接口tag")
@RestController
@RequestMapping("/capi/user")
public class UserController {

    @Autowired
    private MQProducter mqProducter;

    @GetMapping("sendMsg")
    @Transactional
    public void send(String topic,String msg){
        Message<String> build = MessageBuilder.withPayload(msg).build();
//        rocketMQTemplate.send(topic, build);
        mqProducter.sendMessage(topic, build, 123);
    }

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @GetMapping("/userInfo")
    @ApiOperation(value = "获取用户信息")
    public ApiResult<UserInfoResp> getUserInfo() {
        return ApiResult.success(userService.getUserInfo(RequestHolder.getThreadLocal().getUid()));
    }


    @PutMapping("/name")
    @ApiOperation(value = "修改昵称")
    public ApiResult<Void> modifyName(@Valid @RequestBody ModifyNameReq modifyNameReq) {
        userService.modifyName(RequestHolder.getThreadLocal().getUid(), modifyNameReq);
        return ApiResult.success();
    }


    @GetMapping("/badges")
    @ApiOperation(value = "可选徽章预览")
    public ApiResult<List<BadgeResp>> badges() {
        return ApiResult.success(userService.badges(RequestHolder.getThreadLocal().getUid()));
    }

    @PutMapping("/badge")
    @ApiOperation(value = "佩戴徽章")
    public ApiResult<List<Void>> wearingBadge(@Valid @RequestBody BadgeReq badgeReq) {
        userService.wearingBadge(RequestHolder.getThreadLocal().getUid(), badgeReq);
        return ApiResult.success();
    }

    @PutMapping("/black")
    @ApiOperation("拉黑用户")
    public ApiResult<Void> black(@Valid @RequestBody BlackReq req) {
        Long uid = RequestHolder.getThreadLocal().getUid();
        boolean hasPower = roleService.hasPower(uid, RoleEnum.ADMIN);
        AssertUtil.isTrue(hasPower, "没有权限");
        userService.black(req);
        return ApiResult.success();
    }

    @PostMapping("/public/summary/userInfo/batch")
    @ApiOperation(value = "用户信息懒加载")
    public ApiResult<List<SummeryInfoDTO>> userInfoBatch(@Valid @RequestBody SummeryInfoReq summeryInfoReq) {
        return ApiResult.success(userService.getSummeryUserInfo(summeryInfoReq));
    }

    @PostMapping("/public/badges/batch")
    @ApiOperation(value = "徽章信息懒加载")
    public ApiResult<List<ItemInfoDTO>> badgesBatch(@Valid @RequestBody ItemInfoReq req) {
        return ApiResult.success(userService.getItemInfo(req));
    }
}

